//------------------------------------------------------------------------------
#include <QVariant>
#include <QSqlRecord>
#include "CFindResultFrm.h"
#include "CFindAvanceFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CFindAvanceFrm::CFindAvanceFrm(QString table, bool liaison, QWidget *parent) : QDialog(parent) {
	setupUi(this);
	_table=table;
	_result="";
	_requete="";
	_requeteHum="";
	_nbChamp=0;
	
	listChamp=new QStringList();
	_header=new QStringList();
	
	chkLiaison->setCheckState(liaison ? Qt::Checked : Qt::Unchecked);
	loadChamps(liaison);
}
//------------------------------------------------------------------------------
void CFindAvanceFrm::loadChamps(bool liaisons) {
	int y;
	int idx;
	QStringList combinaison;
	QVector<CMySQL::SChampRecherche> vector=CMySQL::getInstance()->getChampsRecherche(_table, liaisons);
	QVector<CMySQL::SChampRecherche>::iterator i;
	QGridLayout *gLayout=(QGridLayout *)gridWidget->layout();
	
	if(liaisons) {
		QString requete="SELECT tableDest FROM rechercheLier WHERE tableSource=\""+_table+"\"";
		QSqlQuery *query;
		
		if(CMySQL::getInstance()->execQuery(requete, &query)) {
			while(query->next()) {
				QSqlRecord rec=query->record();
				vector = vector << CMySQL::getInstance()->getChampsRecherche(rec.value("tableDest").toString(), true);
			}
		}
		delete query;
	}
	
	i=vector.begin();
	_nbChamp=vector.size();
	
	combinaison << tr("Et") << tr("Ou");
	
	chkActif=new QCheckBox*[_nbChamp];
	cbCombinaison=new QComboBox*[_nbChamp];
	leParO=new QLineEdit*[_nbChamp];
	lblChamp=new QLabel*[_nbChamp];
	cbOperation=new QComboBox*[_nbChamp];
	leValeur=new QLineEdit*[_nbChamp];
	leParF=new QLineEdit*[_nbChamp];
	
	idx=0;
	y=1;
	while(i != vector.end()) {
		chkActif[idx]=new QCheckBox(this);
		chkActif[idx]->setCheckState(Qt::Checked);
		chkActif[idx]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
		chkActif[idx]->setMaximumSize(50, HIGHT_CTRL);
		gLayout->addWidget(chkActif[idx], y, 0);
		
		cbCombinaison[idx]=new QComboBox(this);
		cbCombinaison[idx]->addItems(combinaison);
		cbCombinaison[idx]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
		cbCombinaison[idx]->setMaximumSize(100, HIGHT_CTRL);
		gLayout->addWidget(cbCombinaison[idx], y, 1);
		
		leParO[idx]=new QLineEdit(this);
		leParO[idx]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
		leParO[idx]->setMaximumSize(30, HIGHT_CTRL);
		gLayout->addWidget(leParO[idx], y, 2);
		
		lblChamp[idx]=new QLabel((*i).champDescription, this);
		_header->append((*i).champDescription);
		lblChamp[idx]->setFrameShape(QFrame::StyledPanel);
		lblChamp[idx]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
		lblChamp[idx]->setMaximumSize(150, HIGHT_CTRL);
		gLayout->addWidget(lblChamp[idx], y, 3);
		
		cbOperation[idx]=new QComboBox(this);
		fillOperation(cbOperation[idx]);
		cbOperation[idx]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
		cbOperation[idx]->setMaximumSize(100, HIGHT_CTRL);
		gLayout->addWidget(cbOperation[idx], y, 4);
		
		leValeur[idx]=new QLineEdit(this);
		gLayout->addWidget(leValeur[idx], y, 5);
		
		leParF[idx]=new QLineEdit(this);
		leParF[idx]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
		leParF[idx]->setMaximumSize(30, HIGHT_CTRL);
		gLayout->addWidget(leParF[idx], y, 6);
		
		listChamp->append((*i).champNom);
		y++;
		idx++;
		i++;
	}
	
	resize(width(), 0);

}
//------------------------------------------------------------------------------
void CFindAvanceFrm::fillOperation(QComboBox *cb) {
	cb->addItem(tr("Contient"), QVariant("like \"\%%1\%\""));
	cb->addItem(tr("Commence par"), QVariant("like \"%1\%\""));
	cb->addItem(tr("Se termine par"), QVariant("like \"\%%1\""));
	cb->addItem(tr("Est égal à"), QVariant(" = \"%1\""));
	cb->addItem(tr("Est différent de"), QVariant(" <> \"%1\""));
	cb->addItem(tr("Est supérieur à"), QVariant(" > \"%1\""));
	cb->addItem(tr("Est inférieur à"), QVariant(" < \"%1\""));
}
//------------------------------------------------------------------------------
void CFindAvanceFrm::deleteChamps(void) {
	int i;

	for(i=0;i<_nbChamp;i++) {
		delete chkActif[i];
		delete cbCombinaison[i];
		delete leParO[i];
		delete lblChamp[i];
		delete cbOperation[i];
		delete leValeur[i];
		delete leParF[i];
	}
	
	delete[] chkActif;
	delete[] cbCombinaison;
	delete[] leParO;
	delete[] lblChamp;
	delete[] cbOperation;
	delete[] leValeur;
	delete[] leParF;
	
	listChamp->clear();
	
	_header->clear();
	
	resize(size().width(), size().height()-_nbChamp*HIGHT);
	_nbChamp=0;
}
//------------------------------------------------------------------------------
void CFindAvanceFrm::accept(void) {
	QString champs="";
	QString cond="";
	QString group="";
	int i;
	
	for(i=0;i<_nbChamp;i++) {
		if(champs != "") champs+=", ";
		champs+=listChamp->at(i);
		
		if(chkActif[i]->checkState() == Qt::Checked) {
			if(cond != "") {
				cond+=cbCombinaison[i]->currentText() == "Et" ? " AND " : " OR ";
				_requeteHum+=" "+cbCombinaison[i]->currentText()+" ";
			}
			cond+=leParO[i]->text()+listChamp->at(i) + " "+cbOperation[i]->itemData(cbOperation[i]->currentIndex()).toString().arg(leValeur[i]->text())+leParF[i]->text();
			_requeteHum+=leParO[i]->text()+lblChamp[i]->text()+" "+cbOperation[i]->currentText()+" '"+leValeur[i]->text()+"'"+leParF[i]->text();
		}
	}
	
	_requete="SELECT "+champs+" FROM "+_table;
	
	if(chkLiaison->checkState() == Qt::Checked) {
		QString requete="SELECT champSource, tableDest, champDest FROM rechercheLier WHERE tableSource=\""+_table+"\"";
		QSqlQuery *query;
		
		if(CMySQL::getInstance()->execQuery(requete, &query)) {
			while(query->next()) {
				QSqlRecord rec=query->record();
				
				_requete+=" INNER JOIN "+rec.value("tableDest").toString()+" ON "+_table+"."+rec.value("champSource").toString()+"="+rec.value("tableDest").toString()+"."+rec.value("champDest").toString();
			}
		}
		delete query;
		
		group=" GROUP BY "+listChamp->at(0);
	}
	
	if(cond != "") {
		_requete+=" WHERE "+cond;
	}
	_requete+=group;
	
	setResult(QDialog::Accepted);
	hide();
}
//------------------------------------------------------------------------------
void CFindAvanceFrm::on_chkLiaison_clicked(bool checked) {
	deleteChamps();
	loadChamps(checked);
}
//------------------------------------------------------------------------------
void CFindAvanceFrm::on_chkAll_stateChanged(int state) {
	int i;

	for(i=0;i<_nbChamp;i++) {
		chkActif[i]->setCheckState((Qt::CheckState)state);
	}
}
//------------------------------------------------------------------------------
